package main

import (
	"fmt"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	"github.com/silenceper/log"
	"time"
)

var db *gorm.DB

func main() {
	var err error
	address := fmt.Sprintf("%s:%s@tcp(%s:%d)/?charset=utf8mb4,utf8&parseTime=True&loc=Local", "root",
		"root@dev", "172.172.177.20", 33061)
	db, err = gorm.Open("mysql", address)
	if err != nil {
		log.Fatalf("connect mysql error :%s", err)
	}
	for {
		go func() {
			db := getOrm()
			selectTest(db)
		}()
		time.Sleep(time.Millisecond)
	}

}

func getOrm() *gorm.DB {
	db.LogMode(true)
	return db
}

func selectTest(db *gorm.DB) {
	c := 0
	//tx := db.Begin()
	err := db.Exec("use mysql").Error
	if err != nil {
		fmt.Println(err)
	}
	err = db.Table("user").Count(&c).Error
	if err != nil {
		fmt.Printf("%+v\n", db.DB().Stats())
		fmt.Println(err)
	}
	fmt.Printf("%+v\n", db.DB().Stats())
	//tx.Commit()
}
